package com.lazily.try_;

/**
 * @Author: GZQ
 * @date: 2025/2/23 15:12
 * @description: 练习2
 */

public class TryCatchExercise02 {

	// 程序入口
	public static void main(String[] args) {
		System.out.println(method()); // 4
	}

	public static int method() {
		int i = 1;
		try {
			i++; // i = 2
			String[] names = new String[3];
			if (names[1].equals("tom")) { // 空指针
				System.out.println(names[1]);
			} else {
				names[3] = "jack";
			}
			return 1;
		} catch (ArrayIndexOutOfBoundsException e) {
			return 2;
		} catch (NullPointerException e) { // 捕获
			return ++i; // i = 3
		} finally { // 必须执行
			return ++i;
		}
	}
}